package com.wwk.phone.controller;

import com.wwk.phone.entity.vo.ThingVo;
import com.wwk.phone.exceptionhandler.MyException;
import com.wwk.phone.service.ThingService;
import com.wwk.phone.utils.JwtUtils;
import com.wwk.phone.utils.R;
import com.wwk.phone.utils.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 自己的代办事件
 *
 * @author makejava
 * @since 2023-06-05 14:59:57
 */
@RestController
@RequestMapping("thing")
@CrossOrigin
public class ThingController {
    /**
     * 服务对象
     */
    @Resource
    private ThingService thingService;

    /**
     * 条件查询分页方法
     */
    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageThingCondition/{current}/{limit}")
    public R pageThingCondition(@PathVariable("current") Long current,
                                @PathVariable("limit") Long limit,
                                @RequestBody(required = false) ThingVo thingVo,
                                HttpServletRequest request) {
        //防止前端条件为null时，动态sql不查询shield，在xml里修复了这个bug
//        String userId = JwtUtils.getMemberIdByJwtToken(request);
        String userId = "1";
        if (current <= 0) {
            throw new MyException(ResultCode.ERROR, "获取页数不能小于0");
        }
        return thingService.pageThingCondition(current, limit, thingVo, userId);
    }

    /**
     * 通过事件id取消通信录事件
     *
     * @return
     */
    @DeleteMapping("{id}")
    public R cancelById(@PathVariable String id) {
        Long cancelId = Long.valueOf(id);
        thingService.cancelById(cancelId);
        return R.ok();
    }

    /**
     * 通过事件id完成通信录事件
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R competeById(@PathVariable String id) {
        Long thingId = Long.valueOf(id);
        thingService.competeById(thingId);
        return R.ok();
    }

    /**
     * 通过事件id完成通信录事件
     *
     * @return
     */
    @PostMapping()
    public R addThing(@RequestBody ThingVo thing, HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        thingService.addThing(thing, userId);
        return R.ok();
    }

}

